"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.MenuWebLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _configContext = require("../../../extends/configContext");

var _classnames = _interopRequireDefault(require("classnames"));

var _MenuContext = require("../MenuContext");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var MenuWebLayout = /*#__PURE__*/function (_Component) {
  _inherits(MenuWebLayout, _Component);

  var _super = _createSuper(MenuWebLayout);

  function MenuWebLayout(props) {
    var _props$defaultSubmenu, _props$defaultActives;

    var _this;

    _classCallCheck(this, MenuWebLayout);

    _this = _super.call(this, props); // 初始化默认选中
    // 多选

    var initCurrentActiveSubMenuIndexSet = new Set();
    var initCurrentActiveItemIndexSet = new Set(); // 单选

    var initCurrentActiveSubMenuIndex = props !== null && props !== void 0 && (_props$defaultSubmenu = props.defaultSubmenuActives) !== null && _props$defaultSubmenu !== void 0 && _props$defaultSubmenu.length ? props.defaultSubmenuActives[0] : _MenuContext.INITIAL_VALUE;
    var initCurrentActiveItemIndex = props !== null && props !== void 0 && (_props$defaultActives = props.defaultActives) !== null && _props$defaultActives !== void 0 && _props$defaultActives.length ? props.defaultActives[0] : _MenuContext.INITIAL_VALUE;

    if (props.multiple) {
      var _props$defaultSubmenu2, _props$defaultActives2;

      (props === null || props === void 0 ? void 0 : (_props$defaultSubmenu2 = props.defaultSubmenuActives) === null || _props$defaultSubmenu2 === void 0 ? void 0 : _props$defaultSubmenu2.length) && (initCurrentActiveSubMenuIndexSet = new Set(props.defaultSubmenuActives));
      (props === null || props === void 0 ? void 0 : (_props$defaultActives2 = props.defaultActives) === null || _props$defaultActives2 === void 0 ? void 0 : _props$defaultActives2.length) && (initCurrentActiveItemIndexSet = new Set(props.defaultActives));
    }

    _this.state = {
      hoverSubMenuIndex: _MenuContext.INITIAL_VALUE,
      setHoverSubMenuIndex: _this.setHoverSubMenuIndex.bind(_assertThisInitialized(_this)),
      currentActiveSubMenuIndex: initCurrentActiveSubMenuIndex,
      setCurrentActiveSubMenuIndex: _this.setCurrentActiveSubMenuIndex.bind(_assertThisInitialized(_this)),
      currentActiveItemIndex: initCurrentActiveItemIndex,
      setCurrentActiveItemIndex: _this.setCurrentActiveItemIndex.bind(_assertThisInitialized(_this)),
      uniqueOpened: props.uniqueOpened,
      mode: props.mode,
      menuTrigger: props.menuTrigger,
      // 多选相关
      multiple: props.multiple,
      currentActiveSubMenuIndexSet: initCurrentActiveSubMenuIndexSet,
      currentActiveItemIndexSet: initCurrentActiveItemIndexSet,
      setCurrentActiveSubMenuIndexSet: _this.setCurrentActiveSubMenuIndexSet.bind(_assertThisInitialized(_this)),
      setCurrentActiveItemIndexSet: _this.setCurrentActiveItemIndexSet.bind(_assertThisInitialized(_this)),
      // 默认展开
      defaultOpeneds: props.defaultOpeneds
    };
    return _this;
  }

  _createClass(MenuWebLayout, [{
    key: "setHoverSubMenuIndex",
    value: function setHoverSubMenuIndex(index) {
      this.setState({
        hoverSubMenuIndex: index
      });
    }
  }, {
    key: "setCurrentActiveSubMenuIndex",
    value: function setCurrentActiveSubMenuIndex(index) {
      this.setState({
        currentActiveSubMenuIndex: index
      });
    }
  }, {
    key: "setCurrentActiveItemIndex",
    value: function setCurrentActiveItemIndex(index) {
      this.setState({
        currentActiveItemIndex: index
      });
    }
  }, {
    key: "setCurrentActiveItemIndexSet",
    value: function setCurrentActiveItemIndexSet(currentActiveItemIndexSet) {
      this.setState({
        currentActiveItemIndexSet: currentActiveItemIndexSet
      });
    }
  }, {
    key: "setCurrentActiveSubMenuIndexSet",
    value: function setCurrentActiveSubMenuIndexSet(currentActiveSubMenuIndexSet) {
      this.setState({
        currentActiveSubMenuIndexSet: currentActiveSubMenuIndexSet
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
          style = _this$props.style,
          className = _this$props.className,
          mode = _this$props.mode;
      var clsPrefix = this.context.clsPrefix;
      var componentPrefix = "".concat(clsPrefix, "-menu");
      var classNameStr = (0, _classnames.default)(componentPrefix, className, _defineProperty({}, "".concat(componentPrefix, "--horizontal"), mode === 'horizontal'));
      return /*#__PURE__*/_react.default.createElement("ul", {
        className: classNameStr,
        style: style
      }, /*#__PURE__*/_react.default.createElement(_MenuContext.MenuContext.Provider, {
        value: this.state
      }, this.props.children));
    }
  }]);

  return MenuWebLayout;
}(_react.Component);

exports.MenuWebLayout = MenuWebLayout;

_defineProperty(MenuWebLayout, "contextType", _configContext.ConfigContext);